home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / emacs-complete / fsf / emacs / lisp / texnfo-upd.el < prev    next >
Text File  |  1994-05-09  |  79KB  |  2,048 lines

  1. ;;; Texinfo mode utilities for updating nodes and menus in Texinfo files.
  2. ;;; Copyright 1989, 1990, 1991, 1992 Free Software Foundation
  3.  
  4. ;; Author: Robert J. Chassell      
  5. ;; Maintainer: bug-texinfo@prep.ai.mit.edu
  6. ;; Keywords: maint, tex, docs
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;;; Known bug: update commands fail to ignore @ignore.
  27.  
  28. ;;; Summary: how to use the updating commands
  29.  
  30. ; The node and menu updating functions automatically
  31.  
  32. ;   * insert missing `@node' lines,
  33. ;   * insert the `Next', `Previous' and `Up' pointers of a node,
  34. ;   * insert or update the menu for a section, 
  35. ;   * create a master menu for a Texinfo source file.
  36. ;
  37. ; Passed an argument, the `texinfo-update-node' and
  38. ; `texinfo-make-menu' functions do their jobs in the region.
  39. ;
  40. ; In brief, the functions for creating or updating nodes and menus, are:
  41. ;     texinfo-update-node (&optional region-p)            
  42. ;     texinfo-every-node-update ()                        
  43. ;     texinfo-sequential-node-update (&optional region-p)
  44. ;     texinfo-make-menu (&optional region-p)              
  45. ;     texinfo-all-menus-update ()                         
  46. ;     texinfo-master-menu ()
  47. ;
  48. ;     texinfo-insert-node-lines  (&optional title-p)
  49. ;     texinfo-indent-menu-description (column &optional region-p)
  50.  
  51. ; The `texinfo-column-for-description' variable specifies the column to
  52. ; which menu descriptions are indented. 
  53.  
  54. ; Texinfo file structure
  55. ; ----------------------
  56.  
  57. ; To use the updating commands, you must structure your Texinfo file
  58. ; hierarchically.  Each `@node' line, with the exception of the top
  59. ; node, must be accompanied by some kind of section line, such as an
  60. ; `@chapter' or `@section' line.  Each node-line/section-line
  61. ; combination must look like this:
  62.  
  63. ;      @node    Lists and Tables, Cross References, Structuring, Top
  64. ;      @comment node-name,        next,             previous,    up
  65. ;      @chapter Making Lists and Tables
  66.  
  67. ; or like this (without the `@comment' line):
  68.  
  69. ;      @node    Lists and Tables, Cross References, Structuring, Top
  70. ;      @chapter Making Lists and Tables
  71.  
  72. ; If the file has a `top' node, it must be called `top' or `Top' and
  73. ; be the first node in the file.
  74.  
  75.  
  76. ;;; The update node functions described in detail
  77.  
  78. ; The `texinfo-update-node' function without an argument inserts
  79. ; the correct next, previous and up pointers for the node in which
  80. ; point is located (i.e., for the node preceding point).
  81.  
  82. ; With an argument, the `texinfo-update-node' function inserts the
  83. ; correct next, previous and up pointers for the nodes inside the
  84. ; region.
  85.  
  86. ; It does not matter whether the  `@node' line has pre-existing
  87. ; `Next', `Previous', or `Up' pointers in it.  They are removed.
  88.  
  89. ; The `texinfo-every-node-update' function runs `texinfo-update-node'
  90. ; on the whole buffer.
  91.  
  92. ; The `texinfo-sequential-node-update' function inserts the
  93. ; immediately following and preceding node into the `Next' or
  94. ; `Previous' pointers regardless of their hierarchical level.  This is
  95. ; only useful for certain kinds of text, like a novel, which you go
  96. ; through sequentially.
  97.  
  98.  
  99. ;;; The menu making functions described in detail
  100.  
  101. ; The `texinfo-make-menu' function without an argument creates or
  102. ; updates a menu for the section encompassing the node that follows
  103. ; point.  With an argument, it makes or updates menus for the nodes
  104. ; within or part of the marked region.
  105.  
  106. ; Whenever an existing menu is updated, the descriptions from
  107. ; that menu are incorporated into the new menu.  This is done by copying
  108. ; descriptions from the existing menu to the entries in the new menu
  109. ; that have the same node names.  If the node names are different, the
  110. ; descriptions are not copied to the new menu.
  111.  
  112. ; Menu entries that refer to other Info files are removed since they
  113. ; are not a node within current buffer.  This is a deficiency.
  114.  
  115. ; The `texinfo-all-menus-update' function runs `texinfo-make-menu'
  116. ; on the whole buffer.
  117.  
  118. ; The `texinfo-master-menu' function creates an extended menu located
  119. ; after the top node.  (The file must have a top node.)  The function
  120. ; first updates all the regular menus in the buffer (incorporating the
  121. ; descriptions from pre-existing menus), and then constructs a master
  122. ; menu that includes every entry from every other menu.  (However, the
  123. ; function cannot update an already existing master menu; if one
  124. ; exists, it must be removed before calling the function.)
  125.  
  126. ; The `texinfo-indent-menu-description' function indents every
  127. ; description in the menu following point, to the specified column.
  128. ; Non-nil argument (prefix, if interactive) means indent every
  129. ; description in every menu in the region.  This function does not
  130. ; indent second and subsequent lines of a multi-line description.
  131.  
  132. ; The `texinfo-insert-node-lines' function inserts `@node' before the
  133. ; `@chapter', `@section', and such like lines of a region in a Texinfo
  134. ; file where the `@node' lines are missing.
  135. ; With a non-nil argument (prefix, if interactive), the function not
  136. ; only inserts `@node' lines but also inserts the chapter or section
  137. ; titles as the names of the corresponding nodes; and inserts titles
  138. ; as node names in pre-existing `@node' lines that lack names.
  139. ; Since node names should be more concise than section or chapter
  140. ; titles, node names so inserted will need to be edited manually.
  141.  
  142.  
  143. ;;; Code:
  144.  
  145. ;;; The menu making functions
  146.  
  147. (defun texinfo-make-menu (&optional region-p)
  148.   "Without any prefix argument, make or update a menu.
  149. Make the menu for the section enclosing the node found following point.
  150.  
  151. Non-nil argument (prefix, if interactive) means make or update menus
  152. for nodes within or part of the marked region.
  153.  
  154. Whenever a menu exists, and is being updated, the descriptions that
  155. are associated with node names in the pre-existing menu are
  156. incorporated into the new menu.  Otherwise, the nodes' section titles
  157. are inserted as descriptions."
  158.   
  159.   (interactive "P")
  160.   (if (not region-p)
  161.       (let ((level (texinfo-hierarchic-level)))
  162.         (texinfo-make-one-menu level)
  163.         (message "Done...updated the menu.  You may save the buffer."))
  164.     ;; else
  165.     (message "Making or updating menus in %s... " (buffer-name))
  166.     (let ((beginning (region-beginning))
  167.       (region-end (region-end))
  168.           (level (progn         ; find section type following point
  169.                    (goto-char (region-beginning))
  170.                    (texinfo-hierarchic-level))))
  171.       (if (= region-end beginning)
  172.           (error "Please mark a region!"))
  173.       (save-excursion
  174.         (save-restriction
  175.           (widen)
  176.           
  177.           (while  (texinfo-find-lower-level-node level region-end)
  178.             (setq level (texinfo-hierarchic-level)) ; new, lower level
  179.             (texinfo-make-one-menu level))
  180.           
  181.           (while (and (< (point) region-end)
  182.                       (texinfo-find-higher-level-node level region-end))
  183.             (setq level (texinfo-hierarchic-level))
  184.             (while (texinfo-find-lower-level-node level region-end)
  185.               (setq level (texinfo-hierarchic-level)) ; new, lower level
  186.               (texinfo-make-one-menu level))))))
  187.     (message "Done...updated menus.  You may save the buffer.")))
  188.  
  189. (defun texinfo-make-one-menu (level)
  190.   "Make a menu of all the appropriate nodes in this section.
  191. `Appropriate nodes' are those associated with sections that are 
  192. at the level specified by LEVEL.  Point is left at the end of menu."
  193.   (let*
  194.       ((case-fold-search t)
  195.        (beginning
  196.     (save-excursion
  197.       (goto-char (texinfo-update-menu-region-beginning level))
  198.       (end-of-line)
  199.       (point)))
  200.        (end (texinfo-update-menu-region-end level))
  201.        (first (texinfo-menu-first-node beginning end))
  202.        (node-name (progn
  203.                     (goto-char beginning)
  204.                     (beginning-of-line)
  205.                     (texinfo-copy-node-name)))
  206.        (new-menu-list (texinfo-make-menu-list beginning end level)))
  207.     (if (texinfo-old-menu-p beginning first)
  208.         (progn
  209.           (texinfo-incorporate-descriptions new-menu-list)
  210.           (texinfo-incorporate-menu-entry-names new-menu-list)
  211.           (texinfo-delete-old-menu beginning first)))
  212.     (texinfo-insert-menu new-menu-list node-name)))
  213.  
  214. (defun texinfo-all-menus-update (&optional update-all-nodes-p)
  215.   "Update every regular menu in a Texinfo file.
  216. Update pre-existing master menu, if there is one.
  217.  
  218. If called with a non-nil argument, this function first updates all the
  219. nodes in the buffer before updating the menus."
  220.   (interactive "P")
  221.   (let ((case-fold-search t)
  222.         master-menu-p)
  223.     (save-excursion
  224.       (push-mark (point-max) t)
  225.       (goto-char (point-min))
  226.       (message "Checking for a master menu in %s ... "(buffer-name))
  227.       (save-excursion
  228.         (if (re-search-forward texinfo-master-menu-header nil t)
  229.             ;; Remove detailed master menu listing
  230.             (progn
  231.               (setq master-menu-p t)
  232.               (goto-char (match-beginning 0))
  233.               (let ((end-of-detailed-menu-descriptions
  234.                      (save-excursion     ; beginning of end menu line
  235.                        (goto-char (texinfo-menu-end))
  236.                        (beginning-of-line) (forward-char -1)
  237.                        (point))))
  238.                 (delete-region (point) end-of-detailed-menu-descriptions)))))
  239.       
  240.       (if update-all-nodes-p
  241.           (progn
  242.             (message "Updating all nodes in %s ... " (buffer-name))
  243.             (sleep-for 2)
  244.             (push-mark (point-max) t)
  245.             (goto-char (point-min))
  246.         ;; Using the mark to pass bounds this way
  247.         ;; is kludgy, but it's not worth fixing. -- rms.
  248.         (let ((mark-active t))
  249.           (texinfo-update-node t))))
  250.       
  251.       (message "Updating all menus in %s ... " (buffer-name))        
  252.       (sleep-for 2)
  253.       (push-mark (point-max) t)
  254.       (goto-char (point-min))
  255.       ;; Using the mark to pass bounds this way
  256.       ;; is kludgy, but it's not worth fixing. -- rms.
  257.       (let ((mark-active t))
  258.     (texinfo-make-menu t))
  259.       
  260.       (if master-menu-p
  261.           (progn
  262.             (message "Updating the master menu in %s... " (buffer-name))
  263.             (sleep-for 2)
  264.             (texinfo-master-menu nil))))
  265.     
  266.     (message "Done...updated all the menus.  You may save the buffer.")))
  267.  
  268. (defun texinfo-find-lower-level-node (level region-end)
  269.   "Search forward from point for node at any level lower than LEVEL.
  270. Search is limited to the end of the marked region, REGION-END, 
  271. and to the end of the menu region for the level.
  272.  
  273. Return t if the node is found, else nil.  Leave point at the beginning
  274. of the node if one is found; else do not move point."
  275.   (let ((case-fold-search t))
  276.     (if (and (< (point) region-end)
  277.              (re-search-forward
  278.               (concat
  279.                "\\(^@node\\).*\n"         ; match node line
  280.                "\\(\\(\\(^@c\\).*\n\\)"   ; match comment line, if any
  281.                "\\|"                      ; or
  282.                "\\(^@ifinfo[ ]*\n\\)\\)?" ; ifinfo line, if any
  283.                (eval (cdr (assoc level texinfo-update-menu-lower-regexps))))
  284.               ;; the next higher level node marks the end of this
  285.               ;; section, and no lower level node will be found beyond
  286.               ;; this position even if region-end is farther off
  287.               (texinfo-update-menu-region-end level) 
  288.               t))
  289.         (goto-char (match-beginning 1)))))
  290.  
  291. (defun texinfo-find-higher-level-node (level region-end)
  292.   "Search forward from point for node at any higher level than argument LEVEL.
  293. Search is limited to the end of the marked region, REGION-END.
  294.  
  295. Return t if the node is found, else nil.  Leave point at the beginning
  296. of the node if one is found; else do not move point."
  297.   (let ((case-fold-search t))
  298.     (cond
  299.      ((or (string-equal "top" level) (string-equal "chapter" level))
  300.       (if (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" region-end t)
  301.           (progn (beginning-of-line) t)))
  302.      (t
  303.       (if (re-search-forward
  304.            (concat
  305.             "\\(^@node\\).*\n"              ; match node line
  306.             "\\(\\(\\(^@c\\).*\n\\)"        ; match comment line, if any
  307.             "\\|"                           ; or
  308.             "\\(^@ifinfo[ ]*\n\\)\\)?"      ; ifinfo line, if any
  309.             (eval (cdr (assoc level texinfo-update-menu-higher-regexps))))
  310.            region-end t)
  311.           (progn (beginning-of-line) t))))))
  312.  
  313.  
  314. ;;; Making the list of new menu entries
  315.  
  316. (defun texinfo-make-menu-list (beginning end level)
  317.   "Make a list of node names and their descriptions.
  318. Point is left at the end of the menu region, but the menu is not inserted.
  319.  
  320. First argument is position from which to start making menu list; 
  321. second argument is end of region in which to try to locate entries;
  322. third argument is the level of the nodes that are the entries.
  323.  
  324. Node names and descriptions are dotted pairs of strings.  Each pair is
  325. an element of the list.  If the description does not exist, the
  326. element consists only of the node name."
  327.   (goto-char beginning)
  328.   (let (new-menu-list)
  329.     (while (texinfo-menu-locate-entry-p level end)
  330.       (setq new-menu-list 
  331.             (cons (cons 
  332.                    (texinfo-copy-node-name)
  333.                    (prog1 "" (forward-line 1)))
  334.                    ;; Use following to insert section titles automatically.
  335.                    ;; (texinfo-copy-section-title))
  336.                   new-menu-list)))
  337.     (reverse new-menu-list)))
  338.  
  339. (defun texinfo-menu-locate-entry-p (level search-end)
  340.   "Find a node that will be part of menu for this section.
  341. First argument is a string such as \"section\" specifying the general
  342. hierarchical level of the menu; second argument is a position
  343. specifying the end of the search.
  344.  
  345. The function returns t if the node is found, else nil.  It searches
  346. forward from point, and leaves point at the beginning of the node.
  347.  
  348. The function finds entries of the same type.  Thus `subsections' and
  349. `unnumberedsubsecs' will appear in the same menu."
  350.   (let ((case-fold-search t))
  351.     (if (re-search-forward
  352.          (concat
  353.           "\\(^@node\\).*\n"              ; match node line
  354.           "\\(\\(\\(^@c\\).*\n\\)"        ; match comment line, if any
  355.           "\\|"                           ; or
  356.           "\\(^@ifinfo[ ]*\n\\)\\)?"      ; ifinfo line, if any
  357.           (eval
  358.            (cdr (assoc level texinfo-update-menu-same-level-regexps))))
  359.          search-end
  360.          t)
  361.         (goto-char (match-beginning 1)))))
  362.  
  363. (defun texinfo-copy-node-name ()
  364.   "Return the node name as a string.
  365.  
  366. Start with point at the beginning of the node line; copy the text
  367. after the node command up to the first comma on the line, if any, and
  368. return the text as a string.  Leaves point at the beginning of the
  369. line.  If there is no node name, returns an empty string."
  370.   
  371.   (save-excursion
  372.     (buffer-substring
  373.      (progn (forward-word 1)              ; skip over node command
  374.             (skip-chars-forward " \t")    ; and over spaces
  375.             (point))
  376.      (if (search-forward
  377.           ","
  378.           (save-excursion (end-of-line) (point)) t) ; bound search
  379.          (1- (point))
  380.        (end-of-line) (point)))))
  381.  
  382. (defun texinfo-copy-section-title ()
  383.   "Return the title of the section as a string.
  384. The title is used as a description line in the menu when one does not
  385. already exist.
  386.  
  387. Move point to the beginning of the appropriate section line by going
  388. to the start of the text matched by last regexp searched for, which
  389. must have been done by `texinfo-menu-locate-entry-p'."
  390.  
  391.   ;; could use the same re-search as in `texinfo-menu-locate-entry-p'
  392.   ;; instead of using `match-beginning'; such a variation would be
  393.   ;; more general, but would waste information already collected
  394.  
  395.   (goto-char (match-beginning 7))       ; match section name 
  396.  
  397.   (buffer-substring
  398.    (progn (forward-word 1)              ; skip over section type
  399.           (skip-chars-forward " \t")    ; and over spaces
  400.           (point))
  401.    (progn (end-of-line) (point))))
  402.  
  403.  
  404. ;;; Handling the old menu
  405.  
  406. (defun texinfo-old-menu-p (beginning first)
  407.   "Move point to the beginning of the menu for this section, if any.
  408. Otherwise move point to the end of the first node of this section.
  409. Return t if a menu is found, nil otherwise.
  410.  
  411. First argument is the position of the beginning of the section in which
  412. the menu will be located; second argument is the position of the first
  413. node within the section.
  414.  
  415. If no menu is found, the function inserts two newlines just before the
  416. end of the section, and leaves point there where a menu ought to be."
  417.   (goto-char beginning)
  418.   (if (not (re-search-forward "^@menu" first 'goto-end))
  419.       (progn (insert "\n\n") (forward-line -2) nil)
  420.     t))
  421.  
  422. (defun texinfo-incorporate-descriptions (new-menu-list)
  423.   "Copy the old menu line descriptions that exist to the new menu.
  424.  
  425. Point must be at beginning of old menu.
  426.  
  427. If the node-name of the new menu is found in the old menu, insert the
  428. old description into the new entry.
  429.  
  430. For this function, the new menu is a list made up of lists of dotted
  431. pairs in which the first element of the pair is the node name and the
  432. second element the description.  The new menu is changed destructively.
  433. The old menu is the menu as it appears in the texinfo file."
  434.   
  435.   (let ((new-menu-list-pointer new-menu-list)
  436.         (end-of-menu (texinfo-menu-end)))
  437.     (while new-menu-list
  438.       (save-excursion                   ; keep point at beginning of menu 
  439.         (if (re-search-forward
  440.              ;; Existing nodes can have the form
  441.              ;;     * NODE NAME:: DESCRIPTION
  442.              ;; or
  443.              ;;     * MENU ITEM: NODE NAME.     DESCRIPTION.
  444.              ;; 
  445.              ;; Recognize both when looking for the description.
  446.              (concat "\\* \\("              ; so only menu entries are found
  447.                      (car (car new-menu-list)) "::"
  448.                      "\\|"
  449.                      ".*: " (car (car new-menu-list)) "[.,\t\n]"
  450.                      "\\)"
  451.                      )               ; so only complete entries are found
  452.              end-of-menu
  453.              t) 
  454.             (setcdr (car new-menu-list) 
  455.                     (texinfo-menu-copy-old-description end-of-menu))))
  456.       (setq new-menu-list (cdr new-menu-list))) 
  457.     (setq new-menu-list new-menu-list-pointer)))
  458.  
  459. (defun texinfo-incorporate-menu-entry-names (new-menu-list)
  460.   "Copy any old menu entry names to the new menu.
  461.  
  462. Point must be at beginning of old menu.
  463.  
  464. If the node-name of the new menu entry cannot be found in the old
  465. menu, do nothing.
  466.  
  467. For this function, the new menu is a list made up of lists of dotted
  468. pairs in which the first element of the pair is the node name and the
  469. second element is the description (or nil).
  470.  
  471. If we find an existing menu entry name, we change the first element of
  472. the pair to be another dotted pair in which the car is the menu entry
  473. name and the cdr is the node name.
  474.  
  475. NEW-MENU-LIST is changed destructively.  The old menu is the menu as it
  476. appears in the texinfo file."
  477.   
  478.   (let ((new-menu-list-pointer new-menu-list)
  479.         (end-of-menu (texinfo-menu-end)))
  480.     (while new-menu-list
  481.       (save-excursion                   ; keep point at beginning of menu 
  482.         (if (re-search-forward
  483.              ;; Existing nodes can have the form
  484.              ;;     * NODE NAME:: DESCRIPTION
  485.              ;; or
  486.              ;;     * MENU ITEM: NODE NAME.     DESCRIPTION.
  487.              ;; 
  488.              ;; We're interested in the second case.
  489.              (concat "\\* "              ; so only menu entries are found
  490.                      "\\(.*\\): " (car (car new-menu-list))  "[.,\t\n]")
  491.              end-of-menu
  492.              t)
  493.             (setcar
  494.               (car new-menu-list)  ; replace the node name
  495.               (cons (buffer-substring (match-beginning 1) (match-end 1))
  496.                     (car (car new-menu-list)))))
  497.       (setq new-menu-list (cdr new-menu-list))))
  498.     (setq new-menu-list new-menu-list-pointer)))
  499.  
  500. (defun texinfo-menu-copy-old-description (end-of-menu)
  501.   "Return description field of old menu line as string.
  502. Point must be located just after the node name.  Point left before description.
  503. Single argument, END-OF-MENU, is position limiting search."
  504.   (skip-chars-forward "[:.,\t\n ]+")
  505.   ;; don't copy a carriage return at line beginning with asterisk!
  506.   ;; do copy a description that begins with an `@'!
  507.   ;; !! Known bug: does not copy descriptions starting with ^|\{?* etc.
  508.   (if (and (looking-at "\\(\\w+\\|@\\)")    
  509.            (not (looking-at "\\(^\\* \\|^@end menu\\)")))  
  510.       (buffer-substring
  511.        (point)
  512.        (save-excursion
  513.          (re-search-forward "\\(^\\* \\|^@end menu\\)" end-of-menu t)
  514.          (forward-line -1)
  515.          (end-of-line)                  ; go to end of last description line
  516.          (point)))
  517.     ""))
  518.  
  519. (defun texinfo-menu-end ()
  520.   "Return position of end of menu. Does not change location of point.
  521. Signal an error if not end of menu."
  522.   (save-excursion
  523.     (if (re-search-forward "^@end menu" nil t)
  524.         (point)
  525.       (error "Menu does not have an end."))))
  526.  
  527. (defun texinfo-delete-old-menu (beginning first)
  528.   "Delete the old menu.  Point must be in or after menu.
  529. First argument is position of the beginning of the section in which
  530. the menu will be located; second argument is the position of the first
  531. node within the section."
  532.   ;; No third arg to search, so error if search fails.
  533.   (re-search-backward "^@menu" beginning)
  534.   (delete-region (point)
  535.                  (save-excursion
  536.                    (re-search-forward "^@end menu" first)
  537.                    (point))))
  538.  
  539.  
  540. ;;; Inserting new menu
  541.  
  542. ;; try 32, but perhaps 24 is better
  543. (defvar texinfo-column-for-description 32
  544.   "*Column at which descriptions start in a Texinfo menu.")
  545.  
  546. (defun texinfo-insert-menu (menu-list node-name)
  547.   "Insert formatted menu at point.
  548. Indents the first line of the description, if any, to the value of
  549. texinfo-column-for-description.
  550.  
  551. MENU-LIST has form:
  552.  
  553.     \(\(\"node-name1\" . \"description\"\) 
  554.     \(\"node-name2\" . \"description\"\) ... \)
  555.  
  556. However, the description field might be nil.
  557.  
  558. Also, the node-name field might itself be a dotted pair (call it P) of
  559. strings instead of just a string.  In that case, the car of P
  560. is the menu entry name, and the cdr of P is the node name."
  561.   
  562.   (insert "@menu\n")
  563.   (while menu-list
  564.     ;; Every menu entry starts with a star and a space.
  565.     (insert "* ")
  566.     
  567.     ;; Insert the node name (and menu entry name, if present).
  568.     (let ((node-part (car (car menu-list))))
  569.       (if (stringp node-part)
  570.           ;; "Double colon" entry line; menu entry and node name are the same,
  571.           (insert (format "%s::" node-part))  
  572.         ;; "Single colon" entry line; menu entry and node name are different.
  573.         (insert (format "%s: %s." (car node-part) (cdr node-part)))))
  574.     
  575.     ;; Insert the description, if present.
  576.     (if (cdr (car menu-list))
  577.         (progn
  578.           ;; Move to right place.
  579.           (indent-to texinfo-column-for-description 2) 
  580.           ;; Insert description.
  581.           (insert (format "%s" (cdr (car menu-list))))))  
  582.  
  583.     (insert "\n") ; end this menu entry
  584.     (setq menu-list (cdr menu-list)))
  585.   (insert "@end menu")
  586.   (message 
  587.    "Updated \"%s\" level menu following node: %s ... " level node-name))
  588.  
  589.  
  590. ;;; Starting menu descriptions by inserting titles
  591.  
  592. (defun texinfo-start-menu-description ()
  593.   "In this menu entry, insert the node's section title as a description. 
  594. Position point at beginning of description ready for editing.
  595. Do not insert a title if the line contains an existing description.
  596.  
  597. You will need to edit the inserted text since a useful description
  598. complements the node name rather than repeats it as a title does."
  599.   
  600.   (interactive)
  601.   (let (beginning end node-name title)
  602.     (save-excursion
  603.     (beginning-of-line)  
  604.       (if (search-forward "* " (save-excursion (end-of-line) (point)) t)
  605.           (progn (skip-chars-forward " \t")
  606.                  (setq beginning (point)))
  607.         (error "This is not a line in a menu!"))
  608.       
  609.       (cond
  610.         ;; "Double colon" entry line; menu entry and node name are the same,
  611.        ((search-forward "::" (save-excursion (end-of-line) (point)) t)
  612.         (if (looking-at "[ \t]*[^ \t\n]+")
  613.             (error "Descriptive text already exists."))
  614.         (skip-chars-backward ": \t")
  615.         (setq node-name (buffer-substring beginning (point))))
  616.        
  617.        ;; "Single colon" entry line; menu entry and node name are different.
  618.        ((search-forward ":" (save-excursion (end-of-line) (point)) t)
  619.         (skip-chars-forward " \t")
  620.         (setq beginning (point))
  621.         ;; Menu entry line ends in a period, comma, or tab. 
  622.         (if (re-search-forward "[.,\t]"
  623.                                (save-excursion (forward-line 1) (point)) t)
  624.             (progn
  625.               (if (looking-at "[ \t]*[^ \t\n]+")
  626.                   (error "Descriptive text already exists."))
  627.               (skip-chars-backward "., \t")
  628.               (setq node-name (buffer-substring beginning (point))))
  629.           ;; Menu entry line ends in a return.
  630.           (re-search-forward ".*\n"
  631.                            (save-excursion (forward-line 1) (point)) t)
  632.           (skip-chars-backward " \t\n")
  633.           (setq node-name (buffer-substring beginning (point)))
  634.           (if (= 0 (length node-name))
  635.               (error "No node name on this line.")
  636.             (insert "."))))
  637.        (t (error "No node name on this line.")))
  638.       ;; Search for node that matches node name, and copy the section title.
  639.       (if (re-search-forward
  640.            (concat 
  641.             "^@node[ \t]+"
  642.             node-name
  643.             ".*\n"                             ; match node line
  644.             "\\("
  645.             "\\(\\(^@c \\|^@comment\\).*\n\\)" ; match comment line, if any
  646.             "\\|"                              ; or
  647.             "\\(^@ifinfo[ ]*\n\\)"             ; ifinfo line, if any
  648.             "\\)?")
  649.            nil t)
  650.           (progn
  651.             (setq title
  652.                   (buffer-substring
  653.                    ;; skip over section type
  654.                    (progn (forward-word 1) 
  655.                           ;; and over spaces
  656.                           (skip-chars-forward " \t") 
  657.                           (point))
  658.                    (progn (end-of-line)
  659.                           (skip-chars-backward " \t")
  660.                           (point)))))
  661.         (error "Cannot find node to match node name in menu entry.")))
  662.     ;; Return point to the menu and insert the title.
  663.     (end-of-line)
  664.     (delete-region
  665.      (point)
  666.      (save-excursion (skip-chars-backward " \t") (point)))
  667.     (indent-to texinfo-column-for-description 2)
  668.     (save-excursion (insert title))))
  669.  
  670.  
  671. ;;; Handling description indentation
  672.  
  673. ; Since the make-menu functions indent descriptions, these functions
  674. ; are useful primarily for indenting a single menu specially.
  675.  
  676. (defun texinfo-indent-menu-description (column &optional region-p)
  677.   "Indent every description in menu following point to COLUMN.  
  678. Non-nil argument (prefix, if interactive) means indent every
  679. description in every menu in the region.  Does not indent second and
  680. subsequent lines of a multi-line description."
  681.   
  682.   (interactive
  683.    "nIndent menu descriptions to (column number): \nP")
  684.   (save-excursion
  685.     (save-restriction
  686.       (widen)
  687.       (if (not region-p)
  688.           (progn
  689.             (re-search-forward "^@menu")
  690.             (texinfo-menu-indent-description column)
  691.             (message
  692.              "Indented descriptions in menu.  You may save the buffer."))
  693.         ;;else
  694.         (message "Indenting every menu description in region... ")
  695.         (goto-char (region-beginning))
  696.         (while (and (< (point) (region-end))
  697.                     (texinfo-locate-menu-p))
  698.           (forward-line 1)
  699.           (texinfo-menu-indent-description column))
  700.         (message "Indenting done.  You may save the buffer.")))))
  701.  
  702. (defun texinfo-menu-indent-description (to-column-number)
  703.   "Indent the Texinfo file menu description to TO-COLUMN-NUMBER.
  704. Start with point just after the word `menu' in the `@menu' line and
  705. leave point on the line before the `@end menu' line.  Does not indent
  706. second and subsequent lines of a multi-line description."
  707.   (let* ((beginning-of-next-line (point)))
  708.     (while (< beginning-of-next-line
  709.               (save-excursion     ; beginning of end menu line
  710.                 (goto-char (texinfo-menu-end))
  711.                 (beginning-of-line)
  712.                 (point)))
  713.  
  714.       (if (re-search-forward  "\\* \\(.*::\\|.*: [^.,\t\n]+[.,\t]\\)" 
  715.            (texinfo-menu-end) 
  716.            t)
  717.           (progn
  718.             (let ((beginning-white-space (point)))
  719.               (skip-chars-forward " \t")  ; skip over spaces
  720.               (if (looking-at "\\(@\\|\\w\\)+") ; if there is text
  721.                   (progn
  722.                     ;; remove pre-existing indentation
  723.                     (delete-region beginning-white-space (point))
  724.                     (indent-to-column to-column-number))))))
  725.       ;; position point at beginning of next line
  726.       (forward-line 1)                  
  727.       (setq beginning-of-next-line (point)))))
  728.  
  729.  
  730. ;;; Making the master menu
  731.  
  732. (defun texinfo-master-menu (update-all-nodes-menus-p)
  733.   "Make a master menu for a whole Texinfo file.
  734. Non-nil argument (prefix, if interactive) means first update all
  735. existing nodes and menus.  Remove pre-existing master menu, if there is one.
  736.  
  737. This function creates a master menu that follows the top node.  The
  738. master menu includes every entry from all the other menus.  It
  739. replaces any existing ordinary menu that follows the top node.
  740.  
  741. If called with a non-nil argument, this function first updates all the
  742. menus in the buffer (incorporating descriptions from pre-existing
  743. menus) before it constructs the master menu.
  744.  
  745. The function removes the detailed part of an already existing master
  746. menu.  This action depends on the pre-exisitng master menu using the
  747. standard `texinfo-master-menu-header'.
  748.  
  749. The master menu has the following format, which is adapted from the
  750. recommendation in the Texinfo Manual:
  751.  
  752.    * The first part contains the major nodes in the Texinfo file: the
  753.      nodes for the chapters, chapter-like sections, and the major
  754.      appendices.  This includes the indices, so long as they are in
  755.      chapter-like sections, such as unnumbered sections.
  756.  
  757.    * The second and subsequent parts contain a listing of the other,
  758.      lower level menus, in order.  This way, an inquirer can go
  759.      directly to a particular node if he or she is searching for
  760.      specific information.
  761.  
  762. Each of the menus in the detailed node listing is introduced by the
  763. title of the section containing the menu."
  764.   
  765.   (interactive "P")
  766.   (let ((case-fold-search t))
  767.     (widen)
  768.     (goto-char (point-min))
  769.     
  770.     ;; Move point to location after `top'.
  771.     (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t))
  772.         (error "This buffer needs a Top node!"))
  773.     
  774.     (let ((first-chapter                  
  775.            (save-excursion
  776.              (or (re-search-forward "^@node" nil t)
  777.                  (error "Too few nodes for a master menu!"))
  778.              (point))))
  779.       (if (re-search-forward texinfo-master-menu-header first-chapter t)
  780.           ;; Remove detailed master menu listing
  781.           (progn
  782.             (goto-char (match-beginning 0))
  783.             (let ((end-of-detailed-menu-descriptions
  784.                    (save-excursion     ; beginning of end menu line
  785.                      (goto-char (texinfo-menu-end))
  786.                      (beginning-of-line) (forward-char -1)
  787.                      (point))))
  788.               (delete-region (point) end-of-detailed-menu-descriptions)))))
  789.     
  790.     (if update-all-nodes-menus-p
  791.         (progn
  792.           (message "Making a master menu in %s ...first updating all nodes... "
  793.                    (buffer-name))
  794.           (sleep-for 2)
  795.           (push-mark (point-max) t)
  796.           (goto-char (point-min))
  797.           (texinfo-update-node t)
  798.           
  799.           (message "Updating all menus in %s ... " (buffer-name))        
  800.           (sleep-for 2)
  801.           (push-mark (point-max) t)
  802.           (goto-char (point-min))
  803.           (texinfo-make-menu t)))
  804.     
  805.     (message "Now making the master menu in %s... " (buffer-name))
  806.     (sleep-for 2)
  807.     (goto-char (point-min))
  808.     (texinfo-insert-master-menu-list
  809.      (texinfo-master-menu-list))
  810.     
  811.     ;; Remove extra newlines that texinfo-insert-master-menu-list
  812.     ;; may have inserted.
  813.     
  814.     (save-excursion
  815.       (goto-char (point-min))
  816.       
  817.       (if (re-search-forward texinfo-master-menu-header nil t)
  818.           (progn
  819.             (goto-char (match-beginning 0))
  820.             (insert "\n")
  821.             (delete-blank-lines)
  822.             (goto-char (point-min))))
  823.  
  824.       (re-search-forward "^@menu")
  825.       (forward-line -1)
  826.       (delete-blank-lines)
  827.       
  828.       (re-search-forward "^@end menu")
  829.       (forward-line 1)
  830.       (delete-blank-lines))
  831.     
  832.     (message
  833.      "Done...completed making master menu.  You may save the buffer.")))
  834.  
  835. (defun texinfo-master-menu-list ()
  836.   "Return a list of menu entries and header lines for the master menu.
  837.  
  838. Start with the menu for chapters and indices and then find each
  839. following menu and the title of the node preceding that menu.
  840.  
  841. The master menu list has this form:
  842.  
  843.     \(\(\(... \"entry-1-2\"  \"entry-1\"\) \"title-1\"\)
  844.       \(\(... \"entry-2-2\"  \"entry-2-1\"\) \"title-2\"\)
  845.       ...\)
  846.  
  847. However, there does not need to be a title field."
  848.  
  849.   (let (master-menu-list)
  850.     (while (texinfo-locate-menu-p)
  851.       (setq master-menu-list 
  852.             (cons (list
  853.                    (texinfo-copy-menu)
  854.                    (texinfo-copy-menu-title))
  855.                   master-menu-list)))
  856.     (reverse master-menu-list)))
  857.  
  858. (defun texinfo-insert-master-menu-list (master-menu-list)
  859.   "Format and insert the master menu in the current buffer."
  860.   (goto-char (point-min))
  861.   ;; Insert a master menu only after `Top' node and before next node
  862.   ;; \(or include file if there is no next node\).
  863.   (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t))
  864.       (error "This buffer needs a Top node!"))
  865.   (let ((first-chapter
  866.          (save-excursion (re-search-forward "^@node\\|^@include") (point))))
  867.     (if (not (re-search-forward "^@menu" first-chapter t))
  868.         (error
  869.          "Buffer lacks ordinary `Top' menu in which to insert master.")))
  870.   (beginning-of-line)
  871.   (delete-region      ; buffer must have ordinary top menu
  872.    (point)   
  873.    (save-excursion (re-search-forward "^@end menu") (point)))
  874.   
  875.   (save-excursion                       ; leave point at beginning of menu
  876.     ;; Handle top of menu
  877.     (insert "\n@menu\n")
  878.     ;; Insert chapter menu entries
  879.     (setq this-very-menu-list (reverse (car (car master-menu-list))))
  880.     ;; Tell user what is going on.
  881.     (message "Inserting chapter menu entry: %s ... " this-very-menu-list)
  882.     (while this-very-menu-list
  883.       (insert "* " (car this-very-menu-list) "\n")
  884.       (setq this-very-menu-list (cdr this-very-menu-list)))
  885.     
  886.     (setq master-menu-list (cdr master-menu-list))
  887.     
  888.     ;; Only insert detailed master menu if there is one....
  889.     (if (car (car master-menu-list))
  890.         (insert texinfo-master-menu-header))
  891.     
  892.     ;; Now, insert all the other menus
  893.     
  894.     ;; The menu master-menu-list has a form like this:
  895.     ;; ((("beta"  "alpha") "title-A")
  896.     ;;  (("delta" "gamma") "title-B"))
  897.     
  898.     (while master-menu-list
  899.       
  900.       (message
  901.        "Inserting menu for %s .... " (car (cdr (car master-menu-list))))
  902.       ;; insert title of menu section
  903.       (insert "\n" (car (cdr (car master-menu-list))) "\n\n")
  904.       
  905.       ;; insert each menu entry
  906.       (setq this-very-menu-list (reverse (car (car master-menu-list))))
  907.       (while this-very-menu-list
  908.         (insert "* " (car this-very-menu-list) "\n")
  909.         (setq this-very-menu-list (cdr this-very-menu-list)))
  910.       
  911.       (setq master-menu-list (cdr master-menu-list)))
  912.     
  913.     ;; Finish menu
  914.     (insert "@end menu\n\n")))
  915.  
  916. (defvar texinfo-master-menu-header
  917.   "\n --- The Detailed Node Listing ---\n"
  918.   "String inserted before lower level entries in Texinfo master menu.
  919. It comes after the chapter-level menu entries.")
  920.  
  921. (defun texinfo-locate-menu-p ()
  922.   "Find the next menu in the texinfo file.
  923. If found, leave point after word `menu' on the `@menu' line, and return t.
  924. If a menu is not found, do not move point and return nil."
  925.   (re-search-forward "\\(^@menu\\)" nil t))
  926.  
  927. (defun texinfo-copy-menu-title  ()
  928.   "Return the title of the section preceding the menu as a string.
  929. If such a title cannot be found, return an empty string.  Do not move
  930. point."
  931.   (let ((case-fold-search t))
  932.     (save-excursion
  933.       (if (re-search-backward
  934.            (concat
  935.             "\\(^@top"
  936.             "\\|"                         ; or
  937.             texinfo-section-types-regexp  ; all other section types
  938.             "\\)")
  939.            nil
  940.            t)
  941.           (progn
  942.             (beginning-of-line)
  943.             (forward-word 1)              ; skip over section type
  944.             (skip-chars-forward " \t")    ; and over spaces
  945.             (buffer-substring
  946.              (point)
  947.              (progn (end-of-line) (point))))
  948.         ""))))
  949.  
  950. (defun texinfo-copy-menu ()
  951.   "Return the entries of an existing menu as a list.
  952. Start with point just after the word `menu' in the `@menu' line
  953. and leave point on the line before the `@end menu' line."
  954.   (let* (this-menu-list
  955.          (end-of-menu (texinfo-menu-end)) ; position of end of `@end menu'
  956.          (last-entry (save-excursion      ; position of beginning of
  957.                                           ; last `* ' entry
  958.                       (goto-char end-of-menu)
  959.                       ;; handle multi-line description
  960.                       (if (not (re-search-backward "^\* " nil t))
  961.                           (error "No entries in menu."))
  962.                       (point))))
  963.     (while (< (point) last-entry)
  964.       (if (re-search-forward  "^\* " end-of-menu t)
  965.           (progn
  966.             (setq this-menu-list
  967.                   (cons
  968.                    (buffer-substring 
  969.                     (point)
  970.                     ;; copy multi-line descriptions
  971.                     (save-excursion
  972.                       (re-search-forward "\\(^\* \\|^@e\\)" nil t)
  973.                       (- (point) 3)))
  974.                    this-menu-list)))))
  975.     this-menu-list))
  976.  
  977.  
  978. ;;; Determining the hierarchical level in the texinfo file
  979.  
  980. (defun texinfo-specific-section-type () 
  981.   "Return the specific type of next section, as a string.
  982. For example, \"unnumberedsubsec\".  Return \"top\" for top node.
  983.  
  984. Searches forward for a section.  Hence, point must be before the
  985. section whose type will be found.  Does not move point.  Signal an
  986. error if the node is not the top node and a section is not found."
  987.   (let ((case-fold-search t))
  988.     (save-excursion
  989.       (cond
  990.        ((re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
  991. ;;; Following search limit by cph but causes a bug
  992. ;;;             (save-excursion
  993. ;;;               (end-of-line)
  994. ;;;               (point))
  995.                            nil
  996.                            t)
  997.         "top")
  998.        ((re-search-forward texinfo-section-types-regexp nil t)
  999.         (buffer-substring (progn (beginning-of-line) ; copy its name
  1000.                                  (1+ (point)))
  1001.                           (progn (forward-word 1)
  1002.                                  (point))))
  1003.        (t
  1004.         (error
  1005.          "texinfo-specific-section-type: Chapter or section not found."))))))
  1006.  
  1007. (defun texinfo-hierarchic-level ()
  1008.   "Return the general hierarchal level of the next node in a texinfo file.
  1009. Thus, a subheading or appendixsubsec is of type subsection."
  1010.   (let ((case-fold-search t))
  1011.     (cdr (assoc
  1012.           (texinfo-specific-section-type)
  1013.           texinfo-section-to-generic-alist))))
  1014.  
  1015.  
  1016. ;;; Locating the major positions
  1017.  
  1018. (defun texinfo-update-menu-region-beginning (level)  
  1019.   "Locate beginning of higher level section this section is within.
  1020. Return position of the beginning of the node line; do not move point.
  1021. Thus, if this level is subsection, searches backwards for section node.
  1022. Only argument is a string of the general type of section."
  1023.   (let ((case-fold-search t))
  1024.     ;; !! Known bug: if section immediately follows top node, this
  1025.     ;; returns the beginning of the buffer as the beginning of the
  1026.     ;; higher level section.
  1027.     (cond
  1028.      ((or (string-equal "top" level)
  1029.           (string-equal "chapter" level))
  1030.       (save-excursion
  1031.         (goto-char (point-min))
  1032.         (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t)
  1033.         (beginning-of-line)
  1034.         (point)))
  1035.      (t
  1036.       (save-excursion
  1037.         (re-search-backward
  1038.          (concat
  1039.           "\\(^@node\\).*\n"              ; match node line
  1040.           "\\(\\(\\(^@c\\).*\n\\)"        ; match comment line, if any
  1041.           "\\|"                           ; or
  1042.           "\\(^@ifinfo[ ]*\n\\)\\)?"      ; ifinfo line, if any
  1043.           (eval
  1044.            (cdr (assoc level texinfo-update-menu-higher-regexps))))
  1045.          nil
  1046.          'goto-beginning)
  1047.         (point))))))
  1048.  
  1049. (defun texinfo-update-menu-region-end (level)  
  1050.   "Locate end of higher level section this section is within.
  1051. Return position; do not move point.  Thus, if this level is a
  1052. subsection, find the node for the section this subsection is within.
  1053. If level is top or chapter, returns end of file.  Only argument is a
  1054. string of the general type of section."
  1055.   (let ((case-fold-search t))
  1056.     (save-excursion
  1057.       (if (re-search-forward
  1058.            (concat
  1059.             "\\(^@node\\).*\n"            ; match node line
  1060.             "\\(\\(\\(^@c\\).*\n\\)"      ; match comment line, if any
  1061.             "\\|"                         ; or
  1062.             "\\(^@ifinfo[ ]*\n\\)\\)?"    ; ifinfo line, if any
  1063.             (eval
  1064.              ;; Never finds end of level above chapter so goes to end.
  1065.              (cdr (assoc level texinfo-update-menu-higher-regexps))))
  1066.            nil
  1067.            'goto-end)
  1068.           (match-beginning 1)
  1069.         (point-max)))))
  1070.  
  1071. (defun texinfo-menu-first-node (beginning end)
  1072.   "Locate first node of the section the menu will be placed in.  
  1073. Return position; do not move point.
  1074. The menu will be located just before this position.  
  1075.  
  1076. First argument is the position of the beginning of the section in
  1077. which the menu will be located; second argument is the position of the
  1078. end of that region; it limits the search."
  1079.   
  1080.   (save-excursion
  1081.     (goto-char beginning)
  1082.     (forward-line 1)
  1083.     (re-search-forward "^@node" end t)
  1084.     (beginning-of-line)
  1085.     (point)))
  1086.  
  1087.  
  1088. ;;; Alists and regular expressions for defining hierarchical levels
  1089.  
  1090. (defvar texinfo-section-to-generic-alist
  1091.   '(("top" . "top")
  1092.  
  1093.     ("chapter" . "chapter")
  1094.     ("unnumbered" . "chapter")
  1095.     ("majorheading" . "chapter")
  1096.     ("chapheading" . "chapter")
  1097.     ("appendix" . "chapter")
  1098.     
  1099.     ("section" . "section")
  1100.     ("unnumberedsec" . "section")
  1101.     ("heading" . "section")
  1102.     ("appendixsec" . "section")
  1103.     
  1104.     ("subsection" . "subsection")
  1105.     ("unnumberedsubsec" . "subsection")
  1106.     ("subheading" . "subsection")
  1107.     ("appendixsubsec" . "subsection")
  1108.     
  1109.     ("subsubsection" . "subsubsection")
  1110.     ("unnumberedsubsubsec" . "subsubsection")
  1111.     ("subsubheading" . "subsubsection")
  1112.     ("appendixsubsubsec" . "subsubsection"))
  1113.   "*An alist of specific and corresponding generic Texinfo section types.
  1114. The keys are strings specifying specific types of section; the values
  1115. are strings of their corresponding general types.")
  1116.  
  1117. ;; We used to look for just sub, but that found @subtitle.
  1118. (defvar texinfo-section-types-regexp
  1119.   "^@\\(chapter \\|sect\\|subs\\|subh\\|unnum\\|major\\|chapheading \\|heading \\|appendix\\)"
  1120.   "Regexp matching chapter, section, other headings (but not the top node).")
  1121.  
  1122. (defvar texinfo-chapter-level-regexp 
  1123.   "chapter\\|unnumbered \\|appendix \\|majorheading\\|chapheading"
  1124.   "Regular expression matching just the Texinfo chapter level headings.")
  1125.  
  1126. (defvar texinfo-section-level-regexp 
  1127.   "section\\|unnumberedsec\\|heading \\|appendixsec"
  1128.   "Regular expression matching just the Texinfo section level headings.")
  1129.  
  1130. (defvar texinfo-subsection-level-regexp 
  1131.   "subsection\\|unnumberedsubsec\\|subheading\\|appendixsubsec"
  1132.   "Regular expression matching just the Texinfo subsection level headings.")
  1133.  
  1134. (defvar texinfo-subsubsection-level-regexp
  1135.   "subsubsection\\|unnumberedsubsubsec\\|subsubheading\\|appendixsubsubsec"
  1136.   "Regular expression matching just the Texinfo subsubsection level headings.")
  1137.  
  1138. (defvar texinfo-update-menu-same-level-regexps
  1139.   '(("top" . "top[ \t]+")
  1140.     ("chapter" . 
  1141.      (concat "\\(^@\\)\\(" texinfo-chapter-level-regexp "\\)[ \t]*"))
  1142.     ("section" . 
  1143.      (concat "\\(^@\\)\\(" texinfo-section-level-regexp "\\)[ \t]*"))
  1144.     ("subsection" .  
  1145.      (concat "\\(^@\\)\\(" texinfo-subsection-level-regexp "\\)[ \t]+"))
  1146.     ("subsubsection" . 
  1147.      (concat "\\(^@\\)\\(" texinfo-subsubsection-level-regexp "\\)[ \t]+")))
  1148.   "*Regexps for searching for same level sections in a Texinfo file.
  1149. The keys are strings specifying the general hierarchical level in the
  1150. document; the values are regular expressions.")
  1151.  
  1152. (defvar texinfo-update-menu-higher-regexps
  1153.   '(("top" . "^@node [ \t]*DIR") 
  1154.     ("chapter" . "^@node [ \t]*top[ \t]*\\(,\\|$\\)")
  1155.     ("section" .
  1156.      (concat 
  1157.       "\\(^@\\("
  1158.       texinfo-chapter-level-regexp
  1159.       "\\)[ \t]*\\)"))
  1160.     ("subsection" .
  1161.      (concat 
  1162.       "\\(^@\\("
  1163.       texinfo-section-level-regexp
  1164.       "\\|"
  1165.       texinfo-chapter-level-regexp
  1166.       "\\)[ \t]*\\)"))
  1167.     ("subsubsection" .
  1168.      (concat 
  1169.       "\\(^@\\("
  1170.       texinfo-subsection-level-regexp
  1171.       "\\|"
  1172.       texinfo-section-level-regexp
  1173.       "\\|"
  1174.       texinfo-chapter-level-regexp
  1175.       "\\)[ \t]*\\)")))
  1176.   "*Regexps for searching for higher level sections in a Texinfo file.
  1177. The keys are strings specifying the general hierarchical level in the
  1178. document; the values are regular expressions.")
  1179.  
  1180. (defvar texinfo-update-menu-lower-regexps
  1181.   '(("top" . 
  1182.      (concat 
  1183.       "\\(^@\\("
  1184.       texinfo-chapter-level-regexp
  1185.       "\\|"
  1186.       texinfo-section-level-regexp
  1187.       "\\|"
  1188.       texinfo-subsection-level-regexp
  1189.       "\\|"
  1190.       texinfo-subsubsection-level-regexp
  1191.       "\\)[ \t]*\\)"))
  1192.     ("chapter" . 
  1193.      (concat 
  1194.       "\\(^@\\("
  1195.       texinfo-section-level-regexp
  1196.       "\\|"
  1197.       texinfo-subsection-level-regexp
  1198.       "\\|"
  1199.       texinfo-subsubsection-level-regexp
  1200.       "\\)[ \t]*\\)"))
  1201.     ("section" .
  1202.      (concat 
  1203.       "\\(^@\\("
  1204.       texinfo-subsection-level-regexp
  1205.       "\\|"
  1206.       texinfo-subsubsection-level-regexp
  1207.       "\\)[ \t]+\\)"))
  1208.     ("subsection" .
  1209.      (concat 
  1210.       "\\(^@\\("
  1211.       texinfo-subsubsection-level-regexp
  1212.       "\\)[ \t]+\\)"))
  1213.     ("subsubsection" . "nothing lower"))
  1214.   "*Regexps for searching for lower level sections in a Texinfo file.
  1215. The keys are strings specifying the general hierarchical level in the
  1216. document; the values are regular expressions.")
  1217.  
  1218.  
  1219. ;;; Updating a node
  1220.  
  1221. ;;;###autoload
  1222. (defun texinfo-update-node (&optional region-p)
  1223.   "Without any prefix argument, update the node in which point is located.
  1224. Non-nil argument (prefix, if interactive) means update the nodes in the
  1225. marked region.
  1226.  
  1227. The functions for creating or updating nodes and menus, and their
  1228. keybindings, are:
  1229.  
  1230.     texinfo-update-node (&optional region-p)    \\[texinfo-update-node]
  1231.     texinfo-every-node-update ()                \\[texinfo-every-node-update]
  1232.     texinfo-sequential-node-update (&optional region-p)
  1233.  
  1234.     texinfo-make-menu (&optional region-p)      \\[texinfo-make-menu]
  1235.     texinfo-all-menus-update ()                 \\[texinfo-all-menus-update]
  1236.     texinfo-master-menu ()
  1237.  
  1238.     texinfo-indent-menu-description (column &optional region-p)
  1239.  
  1240. The `texinfo-column-for-description' variable specifies the column to
  1241. which menu descriptions are indented. Its default value is 32."
  1242.   
  1243.   (interactive "P")
  1244.   (if (not region-p)
  1245.       ;; update a single node
  1246.       (let ((auto-fill-function nil) (auto-fill-hook nil))
  1247.         (if (not (re-search-backward "^@node" (point-min) t))
  1248.             (error "Node line not found before this position."))
  1249.         (texinfo-update-the-node)
  1250.         (message "Done...updated the node.  You may save the buffer."))
  1251.     ;; else
  1252.     (let ((auto-fill-function nil)
  1253.       (auto-fill-hook nil)
  1254.           (beginning (region-beginning))
  1255.       (end (region-end)))
  1256.       (if (= end beginning)
  1257.           (error "Please mark a region!"))
  1258.       (save-restriction
  1259.     (narrow-to-region beginning end)
  1260.     (goto-char beginning)
  1261.         (push-mark (point) t)
  1262.     (while (re-search-forward "^@node" (point-max) t)
  1263.           (beginning-of-line)            
  1264.           (texinfo-update-the-node))
  1265.         (message "Done...updated nodes in region.  You may save the buffer.")))))
  1266.  
  1267. ;;;###autoload
  1268. (defun texinfo-every-node-update ()
  1269.   "Update every node in a Texinfo file."
  1270.   (interactive)
  1271.   (save-excursion
  1272.     (push-mark (point-max) t)
  1273.     (goto-char (point-min))
  1274.     ;; Using the mark to pass bounds this way
  1275.     ;; is kludgy, but it's not worth fixing. -- rms.
  1276.     (let ((mark-active t))
  1277.       (texinfo-update-node t))
  1278.     (message "Done...updated every node.       You may save the buffer.")))
  1279.  
  1280. (defun texinfo-update-the-node ()
  1281.   "Update one node.  Point must be at the beginning of node line.  
  1282. Leave point at the end of the node line."
  1283.   (texinfo-check-for-node-name)
  1284.   (texinfo-delete-existing-pointers)
  1285.   (message "Updating node: %s ... " (texinfo-copy-node-name))
  1286.   (save-restriction
  1287.     (widen)
  1288.     (let*
  1289.         ((case-fold-search t)
  1290.          (level (texinfo-hierarchic-level))
  1291.          (beginning (texinfo-update-menu-region-beginning level))
  1292.          (end (texinfo-update-menu-region-end level)))
  1293.       (if (string-equal level "top")
  1294.           (texinfo-top-pointer-case)
  1295.         ;; else
  1296.         (texinfo-insert-pointer beginning end level 'next)
  1297.         (texinfo-insert-pointer beginning end level 'previous)
  1298.         (texinfo-insert-pointer beginning end level 'up)
  1299.         (texinfo-clean-up-node-line)))))
  1300.  
  1301. (defun texinfo-top-pointer-case ()
  1302.   "Insert pointers in the Top node.  This is a special case.
  1303.  
  1304. The `Next' pointer is a pointer to a chapter or section at a lower
  1305. hierarchical level in the file.  The `Previous' and `Up' pointers are
  1306. to `(dir)'.  Point must be at the beginning of the node line, and is
  1307. left at the end of the node line."
  1308.  
  1309.   (texinfo-clean-up-node-line)
  1310.   (insert ", " 
  1311.           (save-excursion
  1312.             ;; There may be an @chapter or other such command between
  1313.             ;; the top node line and the next node line, as a title
  1314.             ;; for an `ifinfo' section. This @chapter command must
  1315.             ;; must be skipped.  So the procedure is to search for
  1316.             ;; the next `@node' line, and then copy its name.
  1317.             (if (re-search-forward "^@node" nil t)
  1318.                 (progn
  1319.                   (beginning-of-line)
  1320.                   (texinfo-copy-node-name))
  1321.               " "))
  1322.           ", (dir), (dir)"))
  1323.  
  1324. (defun texinfo-check-for-node-name ()
  1325.   "Determine whether the node has a node name.  Prompt for one if not.
  1326. Point must be at beginning of node line.  Does not move point."
  1327.   (save-excursion
  1328.     (let ((initial (texinfo-copy-next-section-title)))
  1329.       ;; This is not clean.  Use `interactive' to read the arg.
  1330.       (forward-word 1)                    ; skip over node command
  1331.       (skip-chars-forward " \t")          ; and over spaces
  1332.       (if (not (looking-at "[^,\t\n ]+")) ; regexp based on what Info looks for
  1333.                                           ; alternatively, use "[a-zA-Z]+"
  1334.         (let ((node-name
  1335.                (read-from-minibuffer
  1336.                 "Node name (use no @, commas, colons, or apostrophes): "
  1337.                 initial)))
  1338.           (insert " " node-name))))))
  1339.  
  1340. (defun texinfo-delete-existing-pointers ()
  1341.   "Delete `Next', `Previous', and `Up' pointers.  
  1342. Starts from the current position of the cursor, and searches forward
  1343. on the line for a comma and if one is found, deletes the rest of the
  1344. line, including the comma.  Leaves point at beginning of line."
  1345.   (if (search-forward "," (save-excursion (end-of-line) (point)) t)
  1346.       (progn
  1347.         (goto-char (1- (point)))
  1348.         (kill-line nil)))
  1349.   (beginning-of-line))
  1350.  
  1351. (defun texinfo-find-pointer (beginning end level direction)
  1352.   "Move point to section associated with next, previous, or up pointer.
  1353. Return type of pointer (either 'normal or 'no-pointer).
  1354.  
  1355. The first and second arguments bound the search for a pointer to the
  1356. beginning and end, respectively, of the enclosing higher level
  1357. section.  The third argument is a string specifying the general kind
  1358. of section such as \"chapter\ or \"section\".  When looking for the
  1359. `Next' pointer, the section found will be at the same hierarchical
  1360. level in the Texinfo file; when looking for the `Previous' pointer,
  1361. the section found will be at the same or higher hierarchical level in
  1362. the Texinfo file; when looking for the `Up' pointer, the section found
  1363. will be at some level higher in the Texinfo file.  The fourth argument
  1364. \(one of 'next, 'previous, or 'up\) specifies whether to find the
  1365. `Next', `Previous', or `Up' pointer."
  1366.   (let ((case-fold-search t))
  1367.     (cond ((eq direction 'next)
  1368.            (forward-line 3)             ; skip over current node
  1369.            ;; Search for section commands accompanied by node lines;
  1370.            ;; ignore section commands in the middle of nodes.
  1371.            (if (re-search-forward
  1372.                 ;; A `Top' node is never a next pointer, so won't find it.
  1373.                 (concat
  1374.                  ;; Match node line.
  1375.                  "\\(^@node\\).*\n"         
  1376.                  ;; Match comment or ifinfo line, if any
  1377.                  "\\(\\(\\(^@c\\).*\n\\)\\|\\(^@ifinfo[ ]*\n\\)\\)?" 
  1378.                  (eval
  1379.                   (cdr (assoc level texinfo-update-menu-same-level-regexps))))
  1380.                 end
  1381.                 t)
  1382.                'normal
  1383.              'no-pointer))
  1384.           ((eq direction 'previous)
  1385.            (if (re-search-backward
  1386.                 (concat
  1387.                  "\\("
  1388.                  ;; Match node line.
  1389.                  "\\(^@node\\).*\n"         
  1390.                  ;; Match comment or ifinfo line, if any
  1391.                  "\\(\\(\\(^@c\\).*\n\\)\\|\\(^@ifinfo[ ]*\n\\)\\)?" 
  1392.                  (eval
  1393.                   (cdr (assoc level texinfo-update-menu-same-level-regexps)))
  1394.                  "\\|"
  1395.                  ;; Match node line.
  1396.                  "\\(^@node\\).*\n"         
  1397.                  ;; Match comment or ifinfo line, if any
  1398.                  "\\(\\(\\(^@c\\).*\n\\)\\|\\(^@ifinfo[ ]*\n\\)\\)?" 
  1399.                  (eval
  1400.                   (cdr (assoc level texinfo-update-menu-higher-regexps)))
  1401.                  "\\|"
  1402.                  ;; Handle `Top' node specially.
  1403.                  "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
  1404.                  "\\)")
  1405.                 beginning
  1406.                 t)
  1407.                'normal
  1408.              'no-pointer))
  1409.           ((eq direction 'up)
  1410.            (if (re-search-backward
  1411.                 (concat
  1412.                  "\\("
  1413.                  ;; Match node line.
  1414.                  "\\(^@node\\).*\n"         
  1415.                  ;; Match comment or ifinfo line, if any
  1416.                  "\\(\\(\\(^@c\\).*\n\\)\\|\\(^@ifinfo[ ]*\n\\)\\)?" 
  1417.                  (eval (cdr (assoc level texinfo-update-menu-higher-regexps)))
  1418.                  "\\|"
  1419.                  ;; Handle `Top' node specially.
  1420.                  "^@node [ \t]*top[ \t]*\\(,\\|$\\)"
  1421.                  "\\)")
  1422.                 (save-excursion
  1423.                   (goto-char beginning)
  1424.                   (beginning-of-line)
  1425.                   (point))
  1426.                 t)
  1427.                'normal
  1428.              'no-pointer))
  1429.           (t
  1430.            (error "texinfo-find-pointer: lack proper arguments")))))
  1431.  
  1432. (defun texinfo-pointer-name (kind)
  1433.   "Return the node name preceding the section command.
  1434. The argument is the kind of section, either normal or no-pointer."
  1435.   (let (name)
  1436.     (cond ((eq kind 'normal)
  1437.            (end-of-line)                ; this handles prev node top case
  1438.            (re-search-backward          ; when point is already 
  1439.             "^@node"                    ; at the beginning of @node line
  1440.             (save-excursion (forward-line -3))
  1441.             t)
  1442.            (setq name (texinfo-copy-node-name)))
  1443.       ((eq kind 'no-pointer)
  1444.        (setq name " ")))    ; put a blank in the pointer slot
  1445.     name))
  1446.  
  1447. (defun texinfo-insert-pointer (beginning end level direction)
  1448.   "Insert the `Next', `Previous' or `Up' node name at point.
  1449. Move point forward.  
  1450.  
  1451. The first and second arguments bound the search for a pointer to the
  1452. beginning and end, respectively, of the enclosing higher level
  1453. section.  The third argument is the hierarchical level of the Texinfo
  1454. file, a string such as \"section\".  The fourth argument is direction
  1455. towards which the pointer is directed, one of `next, `previous, or
  1456. 'up."
  1457.  
  1458.   (end-of-line)
  1459.   (insert
  1460.    ", "
  1461.    (save-excursion
  1462.      (texinfo-pointer-name
  1463.       (texinfo-find-pointer beginning end level direction)))))
  1464.  
  1465. (defun texinfo-clean-up-node-line ()
  1466.   "Remove extra commas, if any, at end of node line."
  1467.   (end-of-line)
  1468.   (skip-chars-backward ", ")
  1469.   (delete-region (point) (save-excursion (end-of-line) (point))))
  1470.  
  1471.  
  1472. ;;; Updating nodes sequentially
  1473. ; These sequential update functions insert `Next' or `Previous'
  1474. ; pointers that point to the following or preceding nodes even if they
  1475. ; are at higher or lower hierarchical levels.  This means that if a
  1476. ; section contains one or more subsections, the section's `Next'
  1477. ; pointer will point to the subsection and not the following section.
  1478. ; (The subsection to which `Next' points will most likely be the first
  1479. ; item on the section's menu.)
  1480.  
  1481. ;;;###autoload
  1482. (defun texinfo-sequential-node-update (&optional region-p)
  1483.   "Update one node (or many) in a Texinfo file with sequential pointers.
  1484.  
  1485. This function causes the `Next' or `Previous' pointer to point to the
  1486. immediately preceding or following node, even if it is at a higher or
  1487. lower hierarchical level in the document.  Continually pressing `n' or
  1488. `p' takes you straight through the file.
  1489.  
  1490. Without any prefix argument, update the node in which point is located.
  1491. Non-nil argument (prefix, if interactive) means update the nodes in the
  1492. marked region.
  1493.  
  1494. This command makes it awkward to navigate among sections and
  1495. subsections; it should be used only for those documents that are meant
  1496. to be read like a novel rather than a reference, and for which the
  1497. Info `g*' command is inadequate."
  1498.   
  1499.   (interactive "P")
  1500.   (if (not region-p)
  1501.       ;; update a single node
  1502.       (let ((auto-fill-function nil) (auto-fill-hook nil))
  1503.         (if (not (re-search-backward "^@node" (point-min) t))
  1504.             (error "Node line not found before this position."))
  1505.         (texinfo-sequentially-update-the-node)
  1506.         (message 
  1507.          "Done...sequentially updated the node .  You may save the buffer."))
  1508.     ;; else
  1509.     (let ((auto-fill-function nil)
  1510.       (auto-fill-hook nil)
  1511.           (beginning (region-beginning))
  1512.           (end (region-end)))
  1513.       (if (= end beginning)
  1514.           (error "Please mark a region!"))
  1515.       (save-restriction
  1516.         (narrow-to-region beginning end)
  1517.         (goto-char beginning)
  1518.         (push-mark (point) t)
  1519.         (while (re-search-forward "^@node" (point-max) t)
  1520.           (beginning-of-line)            
  1521.           (texinfo-sequentially-update-the-node))
  1522.         (message 
  1523.          "Done...updated the nodes in sequence.  You may save the buffer.")))))
  1524.  
  1525. (defun texinfo-sequentially-update-the-node ()
  1526.   "Update one node such that the pointers are sequential. 
  1527. A `Next' or `Previous' pointer points to any preceding or following node,
  1528. regardless of its hierarchical level."
  1529.  
  1530.         (texinfo-check-for-node-name)
  1531.         (texinfo-delete-existing-pointers)
  1532.         (message 
  1533.          "Sequentially updating node: %s ... " (texinfo-copy-node-name))
  1534.         (save-restriction
  1535.           (widen)
  1536.           (let*
  1537.               ((case-fold-search t)
  1538.                (level (texinfo-hierarchic-level)))
  1539.             (if (string-equal level "top")
  1540.                 (texinfo-top-pointer-case)
  1541.               ;; else
  1542.               (texinfo-sequentially-insert-pointer level 'next)
  1543.               (texinfo-sequentially-insert-pointer level 'previous)
  1544.               (texinfo-sequentially-insert-pointer level 'up)
  1545.               (texinfo-clean-up-node-line)))))
  1546.  
  1547. (defun texinfo-sequentially-find-pointer (level direction)
  1548.   "Find next or previous pointer sequentially in Texinfo file, or up pointer.
  1549. Move point to section associated with the pointer.  Find point even if
  1550. it is in a different section.
  1551.  
  1552. Return type of pointer (either 'normal or 'no-pointer).
  1553.  
  1554. The first argument is a string specifying the general kind of section
  1555. such as \"chapter\ or \"section\".  The section found will be at the
  1556. same hierarchical level in the Texinfo file, or, in the case of the up
  1557. pointer, some level higher.  The second argument (one of 'next,
  1558. 'previous, or 'up) specifies whether to find the `Next', `Previous',
  1559. or `Up' pointer."
  1560.   (let ((case-fold-search t))  
  1561.     (cond ((eq direction 'next)
  1562.            (forward-line 3)             ; skip over current node
  1563.            (if (re-search-forward 
  1564.                 texinfo-section-types-regexp
  1565.                 (point-max)
  1566.                 t)
  1567.                'normal
  1568.              'no-pointer))
  1569.           ((eq direction 'previous)
  1570.            (if (re-search-backward 
  1571.                 texinfo-section-types-regexp
  1572.                 (point-min)
  1573.                 t)
  1574.                'normal
  1575.              'no-pointer))
  1576.           ((eq direction 'up)
  1577.            (if (re-search-backward
  1578.                 (eval (cdr (assoc level texinfo-update-menu-higher-regexps)))
  1579.                 beginning
  1580.                 t)
  1581.                'normal
  1582.              'no-pointer))
  1583.           (t
  1584.            (error "texinfo-sequential-find-pointer: lack proper arguments")))))
  1585.  
  1586. (defun texinfo-sequentially-insert-pointer (level direction)
  1587.   "Insert the `Next', `Previous' or `Up' node name at point.
  1588. Move point forward.  
  1589.  
  1590. The first argument is the hierarchical level of the Texinfo file, a
  1591. string such as \"section\".  The second argument is direction, one of
  1592. `next, `previous, or 'up."
  1593.  
  1594.   (end-of-line)
  1595.   (insert
  1596.    ", "
  1597.    (save-excursion
  1598.      (texinfo-pointer-name
  1599.       (texinfo-sequentially-find-pointer level direction)))))
  1600.  
  1601.  
  1602. ;;; Inserting `@node' lines
  1603. ; The `texinfo-insert-node-lines' function inserts `@node' lines as needed
  1604. ; before the `@chapter', `@section', and such like lines of a region
  1605. ; in a Texinfo file.
  1606.  
  1607. (defun texinfo-insert-node-lines (beginning end &optional title-p)
  1608.   "Insert missing `@node' lines in region of Texinfo file.
  1609. Non-nil argument (prefix, if interactive) means also to insert the
  1610. section titles as node names; and also to insert the section titles as
  1611. node names in pre-existing @node lines that lack names."
  1612.   (interactive "r\nP")
  1613.  
  1614.   ;; Use marker; after inserting node lines, leave point at end of
  1615.   ;; region and mark at beginning.
  1616.  
  1617.   (let (beginning-marker end-marker title last-section-position)
  1618.  
  1619.     ;; Save current position on mark ring and set mark to end.
  1620.     (push-mark end t)                   
  1621.     (setq end-marker (mark-marker))        
  1622.  
  1623.     (goto-char beginning)
  1624.     (while (re-search-forward
  1625.             texinfo-section-types-regexp 
  1626.             end-marker
  1627.             'end)
  1628.       ;; Copy title if desired.
  1629.       (if title-p
  1630.           (progn 
  1631.             (beginning-of-line)
  1632.             (forward-word 1)
  1633.             (skip-chars-forward " \t")
  1634.             (setq title (buffer-substring
  1635.                          (point)
  1636.                          (save-excursion (end-of-line) (point))))))
  1637.       ;; Insert node line if necessary.
  1638.       (if (re-search-backward
  1639.            "^@node" 
  1640.            ;; Avoid finding previous node line if node lines are close.
  1641.            (or last-section-position    
  1642.                (save-excursion (forward-line -2) (point))) t)
  1643.           ;;  @node is present, and point at beginning of that line
  1644.           (forward-word 1)          ; Leave point just after @node.
  1645.         ;; Else @node missing; insert one.
  1646.         (beginning-of-line)         ; Beginning of `@section' line.
  1647.         (insert "@node\n")
  1648.         (backward-char 1))          ; Leave point just after `@node'.
  1649.       ;; Insert title if desired.
  1650.       (if title-p
  1651.           (progn
  1652.             (skip-chars-forward " \t")
  1653.             ;; Use regexp based on what info looks for
  1654.             ;; (alternatively, use "[a-zA-Z]+");
  1655.             ;; this means we only insert a title if none exists.
  1656.             (if (not (looking-at "[^,\t\n ]+")) 
  1657.                 (progn
  1658.                   (beginning-of-line) 
  1659.                   (forward-word 1)
  1660.                   (insert " " title)
  1661.                   (message "Inserted title %s ... " title)))))
  1662.       ;; Go forward beyond current section title.
  1663.       (re-search-forward texinfo-section-types-regexp 
  1664.                          (save-excursion (forward-line 3) (point)) t)
  1665.       (setq last-section-position (point))
  1666.       (forward-line 1))
  1667.  
  1668.     ;; Leave point at end of region, mark at beginning.
  1669.     (set-mark beginning)
  1670.  
  1671.     (if title-p
  1672.       (message
  1673.        "Done inserting node lines and titles.  You may save the buffer.")
  1674.     (message "Done inserting node lines.  You may save the buffer."))))
  1675.  
  1676.  
  1677. ;;; Update and create menus for multi-file Texinfo sources
  1678.  
  1679. ;;  1. M-x texinfo-multiple-files-update 
  1680. ;;
  1681. ;;     Read the include file list of an outer Texinfo file and
  1682. ;;     update all highest level nodes in the files listed and insert a
  1683. ;;     main menu in the outer file after its top node.
  1684.  
  1685. ;;  2. C-u M-x texinfo-multiple-files-update 
  1686. ;;
  1687. ;;     Same as 1, but insert a master menu.  (Saves reupdating lower
  1688. ;;     level menus and nodes.)  This command simply reads every menu,
  1689. ;;     so if the menus are wrong, the master menu will be wrong.
  1690. ;;     Similarly, if the lower level node pointers are wrong, they
  1691. ;;     will stay wrong.
  1692.  
  1693. ;;  3. C-u 2 M-x texinfo-multiple-files-update 
  1694. ;;
  1695. ;;     Read the include file list of an outer Texinfo file and
  1696. ;;     update all nodes and menus in the files listed and insert a
  1697. ;;     master menu in the outer file after its top node.
  1698.  
  1699. ;;; Note: these functions:
  1700. ;;;
  1701. ;;;   * Do not save or delete any buffers.  You may fill up your memory.
  1702. ;;;   * Do not handle any pre-existing nodes in outer file.  
  1703. ;;;     Hence, you may need a file for indices.
  1704.  
  1705.  
  1706. ;;; Auxiliary functions for multiple file updating
  1707.  
  1708. (defun texinfo-multi-file-included-list (outer-file)
  1709.   "Return a list of the included files in OUTER-FILE."
  1710.   (let ((included-file-list (list outer-file))
  1711.         start)
  1712.     (save-excursion
  1713.       (switch-to-buffer (find-file-noselect outer-file))
  1714.       (widen)
  1715.       (goto-char (point-min))
  1716.       (while (re-search-forward "^@include" nil t)
  1717.         (skip-chars-forward " \t")
  1718.         (setq start (point))
  1719.         (end-of-line)
  1720.         (skip-chars-backward " \t")   
  1721.         (setq included-file-list
  1722.               (cons (buffer-substring start (point))
  1723.                     included-file-list)))
  1724.       (nreverse included-file-list))))
  1725.  
  1726. (defun texinfo-copy-next-section-title ()
  1727.   "Return the name of the immediately following section as a string.
  1728.  
  1729. Start with point at the beginning of the node line.  Leave point at the
  1730. same place.  If there is no title, returns an empty string."
  1731.  
  1732.   (save-excursion
  1733.     (end-of-line)
  1734.     (let ((node-end (or 
  1735.                         (save-excursion
  1736.                           (if (re-search-forward "\\(^@node\\)" nil t)
  1737.                               (match-beginning 0)))
  1738.                         (point-max))))
  1739.       (if (re-search-forward texinfo-section-types-regexp node-end t)
  1740.           (progn
  1741.             (beginning-of-line)
  1742.             ;; copy title
  1743.             (let ((title
  1744.                    (buffer-substring
  1745.                     (progn (forward-word 1)           ; skip over section type
  1746.                            (skip-chars-forward " \t") ; and over spaces
  1747.                            (point))
  1748.                     (progn (end-of-line) (point)))))
  1749.               title))
  1750.         ""))))
  1751.  
  1752. (defun texinfo-multi-file-update (files &optional update-everything)
  1753.   "Update first node pointers in each file in FILES.
  1754. Return a list of the node names.
  1755.  
  1756. The first file in the list is an outer file; the remaining are
  1757. files included in the outer file with `@include' commands.
  1758.  
  1759. If optional arg UPDATE-EVERYTHING non-nil, update every menu and
  1760. pointer in each of the included files.
  1761.  
  1762. Also update the `Top' level node pointers of the outer file.
  1763.  
  1764. Requirements:
  1765.  
  1766.   * the first file in the FILES list must be the outer file,
  1767.   * each of the included files must contain exactly one highest
  1768.     hierarchical level node, 
  1769.   * this node must be the first node in the included file,
  1770.   * each highest hierarchical level node must be of the same type.
  1771.  
  1772. Thus, normally, each included file contains one, and only one,
  1773. chapter."
  1774.  
  1775. ; The menu-list has the form:
  1776. ;     \(\(\"node-name1\" . \"title1\"\) 
  1777. ;       \(\"node-name2\" . \"title2\"\) ... \)
  1778. ; However, there does not need to be a title field and this function
  1779. ; does not fill it; however a comment tells you how to do so.
  1780. ; You would use the title field if you wanted to insert titles in the
  1781. ; description slot of a menu as a description.
  1782.   
  1783.   (let ((case-fold-search t)
  1784.         menu-list)
  1785.     
  1786.     ;; Find the name of the first node of the first included file.
  1787.     (switch-to-buffer (find-file-noselect (car (cdr files))))
  1788.     (widen)
  1789.     (goto-char (point-min))
  1790.     (if (not (re-search-forward "^@node" nil t))
  1791.         (error "No `@node' line found in %s !" (buffer-name)))
  1792.     (beginning-of-line)
  1793.     (texinfo-check-for-node-name)
  1794.     (setq next-node-name (texinfo-copy-node-name))
  1795.     
  1796.     (setq menu-list
  1797.           (cons (cons 
  1798.                  next-node-name
  1799.                  (prog1 "" (forward-line 1)))
  1800.                 ;; Use following to insert section titles automatically.
  1801.                 ;; (texinfo-copy-next-section-title)
  1802.                 menu-list))
  1803.  
  1804.     ;; Go to outer file
  1805.     (switch-to-buffer (find-file-noselect (car files)))
  1806.     (goto-char (point-min))
  1807.     (if (not (re-search-forward "^@node [ \t]*top[ \t]*\\(,\\|$\\)" nil t))
  1808.         (error "This buffer needs a Top node!"))
  1809.     (beginning-of-line)
  1810.     (texinfo-delete-existing-pointers)
  1811.     (end-of-line)
  1812.     (insert ", " next-node-name ", (dir), (dir)")
  1813.     (beginning-of-line)
  1814.     (setq previous-node-name "Top")
  1815.     (setq files (cdr files))
  1816.     
  1817.     (while files
  1818.       
  1819.       (if (not (cdr files))
  1820.           ;; No next file
  1821.           (setq next-node-name "")
  1822.         ;; Else,
  1823.         ;; find the name of the first node in the next file.
  1824.         (switch-to-buffer (find-file-noselect (car (cdr files))))
  1825.         (widen)
  1826.         (goto-char (point-min))
  1827.         (if (not (re-search-forward "^@node" nil t))
  1828.             (error "No `@node' line found in %s !" (buffer-name)))
  1829.         (beginning-of-line)
  1830.         (texinfo-check-for-node-name)
  1831.         (setq next-node-name (texinfo-copy-node-name))
  1832.         (setq menu-list
  1833.               (cons (cons 
  1834.                      next-node-name
  1835.                      (prog1 "" (forward-line 1)))
  1836.                     ;; Use following to insert section titles automatically.
  1837.                     ;; (texinfo-copy-next-section-title)
  1838.                     menu-list)))
  1839.  
  1840.       ;; Go to node to be updated.
  1841.       (switch-to-buffer (find-file-noselect (car files)))
  1842.       (goto-char (point-min))
  1843.       (if (not (re-search-forward "^@node" nil t))
  1844.           (error "No `@node' line found in %s !" (buffer-name)))
  1845.       (beginning-of-line)
  1846.       
  1847.       ;; Update other menus and nodes if requested.
  1848.       (if update-everything (texinfo-all-menus-update t))
  1849.  
  1850.       (beginning-of-line)
  1851.       (texinfo-delete-existing-pointers)
  1852.       (end-of-line)
  1853.       (insert ", " next-node-name ", " previous-node-name ", " up-node-name)
  1854.       
  1855.       (beginning-of-line)
  1856.       (setq previous-node-name (texinfo-copy-node-name))
  1857.       
  1858.       (setq files (cdr files)))
  1859.     (nreverse menu-list)))
  1860.  
  1861. (defun texinfo-multi-files-insert-main-menu (menu-list)
  1862.   "Insert formatted main menu at point.
  1863. Indents the first line of the description, if any, to the value of
  1864. texinfo-column-for-description."
  1865.  
  1866.   (insert "@menu\n")
  1867.   (while menu-list
  1868.     ;; Every menu entry starts with a star and a space.
  1869.     (insert "* ")
  1870.     
  1871.     ;; Insert the node name (and menu entry name, if present).
  1872.     (let ((node-part (car (car menu-list))))
  1873.       (if (stringp node-part)
  1874.           ;; "Double colon" entry line; menu entry and node name are the same,
  1875.           (insert (format "%s::" node-part))  
  1876.         ;; "Single colon" entry line; menu entry and node name are different.
  1877.         (insert (format "%s: %s." (car node-part) (cdr node-part)))))
  1878.     
  1879.     ;; Insert the description, if present.
  1880.     (if (cdr (car menu-list))
  1881.         (progn
  1882.           ;; Move to right place.
  1883.           (indent-to texinfo-column-for-description 2) 
  1884.           ;; Insert description.
  1885.           (insert (format "%s" (cdr (car menu-list))))))  
  1886.  
  1887.     (insert "\n") ; end this menu entry
  1888.     (setq menu-list (cdr menu-list)))
  1889.   (insert "@end menu"))
  1890.  
  1891. (defun texinfo-multi-file-master-menu-list (files-list)
  1892.   "Return master menu list from files in FILES-LIST.
  1893. Menu entries in each file collected using `texinfo-master-menu-list'.
  1894.  
  1895. The first file in FILES-LIST must be the outer file; the others must
  1896. be the files included within it.  A main menu must already exist."
  1897.   (save-excursion
  1898.     (let (master-menu-list)
  1899.       (while files-list
  1900.         (switch-to-buffer (find-file-noselect (car files-list)))
  1901.         (message "Working on: %s " (current-buffer))
  1902.         (goto-char (point-min))
  1903.         (setq master-menu-list
  1904.               (append master-menu-list (texinfo-master-menu-list)))
  1905.         (setq files-list (cdr files-list)))
  1906.       master-menu-list)))
  1907.  
  1908.  
  1909. ;;; The multiple-file update function
  1910.  
  1911. (defun texinfo-multiple-files-update
  1912.   (outer-file &optional update-everything make-master-menu)
  1913.   "Update first node pointers in each file included in OUTER-FILE;
  1914. create or update the `Top' level node pointers and the main menu in
  1915. the outer file that refers to such nodes.  This does not create or
  1916. update menus or pointers within the included files.
  1917.  
  1918. With optional MAKE-MASTER-MENU argument (prefix arg, if interactive),
  1919. insert a master menu in OUTER-FILE in addition to creating or updating
  1920. pointers in the first @node line in each included file and creating or
  1921. updating the `Top' level node pointers of the outer file.  This does
  1922. not create or update other menus and pointers within the included
  1923. files.
  1924.  
  1925. With optional UPDATE-EVERYTHING argument (numeric prefix arg, if
  1926. interactive), update all the menus and all the `Next', `Previous', and
  1927. `Up' pointers of all the files included in OUTER-FILE before inserting
  1928. a master menu in OUTER-FILE.  Also, update the `Top' level node
  1929. pointers of OUTER-FILE.
  1930.  
  1931. Notes: 
  1932.  
  1933.   * this command does NOT save any files--you must save the
  1934.     outer file and any modified, included files.
  1935.  
  1936.   * except for the `Top' node, this command does NOT handle any
  1937.     pre-existing nodes in the outer file; hence, indices must be
  1938.     enclosed in an included file.
  1939.  
  1940. Requirements:
  1941.  
  1942.   * each of the included files must contain exactly one highest
  1943.     hierarchical level node, 
  1944.   * this highest node must be the first node in the included file,
  1945.   * each highest hierarchical level node must be of the same type.
  1946.  
  1947. Thus, normally, each included file contains one, and only one,
  1948. chapter."
  1949.   
  1950.   (interactive (cons
  1951.                 (read-string
  1952.                  "Name of outer `include' file: "
  1953.                  (buffer-file-name))
  1954.                 (cond ((not current-prefix-arg)
  1955.                        '(nil nil))
  1956.                       ((listp current-prefix-arg)
  1957.                        '(t nil))   ; make-master-menu 
  1958.                       ((numberp current-prefix-arg)
  1959.                        '(t t))     ; update-everything
  1960.                       )))
  1961.  
  1962.   (let* ((included-file-list (texinfo-multi-file-included-list outer-file))
  1963.          (files included-file-list)
  1964.          main-menu-list
  1965.          next-node-name
  1966.          previous-node-name
  1967.          (up-node-name "Top"))
  1968.  
  1969. ;;; Update the pointers 
  1970. ;;; and collect the names of the nodes and titles
  1971.     (setq main-menu-list (texinfo-multi-file-update files update-everything))
  1972.  
  1973. ;;; Insert main menu
  1974.  
  1975.   ;; Go to outer file
  1976.   (switch-to-buffer (find-file-noselect (car included-file-list)))
  1977.   (if (texinfo-old-menu-p
  1978.        (point-min)
  1979.        (save-excursion
  1980.          (re-search-forward "^@include")
  1981.          (beginning-of-line)
  1982.          (point)))
  1983.  
  1984.       ;; If found, leave point after word `menu' on the `@menu' line.
  1985.       (progn
  1986.         (texinfo-incorporate-descriptions main-menu-list)
  1987.         ;; Delete existing menu.
  1988.         (beginning-of-line)
  1989.         (delete-region
  1990.          (point)
  1991.          (save-excursion (re-search-forward "^@end menu") (point)))
  1992.         ;; Insert main menu
  1993.         (texinfo-multi-files-insert-main-menu main-menu-list))
  1994.  
  1995.     ;; Else no current menu; insert it before `@include'
  1996.     (texinfo-multi-files-insert-main-menu main-menu-list))
  1997.  
  1998. ;;; Insert master menu
  1999.  
  2000.   (if make-master-menu
  2001.       (progn
  2002.         ;; First, removing detailed part of any pre-existing master menu
  2003.         (goto-char (point-min))
  2004.         (if (re-search-forward texinfo-master-menu-header nil t)
  2005.             ;; Remove detailed master menu listing
  2006.             (progn
  2007.               (goto-char (match-beginning 0))
  2008.               (let ((end-of-detailed-menu-descriptions
  2009.                      (save-excursion     ; beginning of end menu line
  2010.                        (goto-char (texinfo-menu-end))
  2011.                        (beginning-of-line) (forward-char -1)
  2012.                        (point))))
  2013.                 (delete-region (point) end-of-detailed-menu-descriptions))))
  2014.  
  2015.         ;; Create a master menu and insert it
  2016.         (texinfo-insert-master-menu-list 
  2017.          (texinfo-multi-file-master-menu-list
  2018.           included-file-list)))))
  2019.  
  2020.   ;; Remove unwanted extra lines.
  2021.   (save-excursion
  2022.     (goto-char (point-min))
  2023.       
  2024.     (re-search-forward "^@menu")
  2025.     (forward-line -1)
  2026.     (insert  "\n") ; Ensure at least one blank line.
  2027.     (delete-blank-lines)
  2028.       
  2029.     (re-search-forward "^@end menu")
  2030.     (forward-line 1)
  2031.     (insert  "\n") ; Ensure at least one blank line.
  2032.     (delete-blank-lines))
  2033.  
  2034.   (message "Multiple files updated."))
  2035.  
  2036.  
  2037. ;;; Place `provide' at end of file.
  2038. (provide 'texnfo-upd)
  2039.  
  2040. ;;; texnfo-upd.el ends here
  2041.